<template>
  <div class="login-container">
    <el-card class="login-box">
      <div class="login-header">
        <img src="../assets/logo.png" alt="logo" class="login-logo">
        <h2 class="login-title">用户管理系统</h2>
      </div>
      <el-form :model="loginForm" :rules="rules" ref="loginForm">
        <el-form-item prop="account">
          <el-input
            v-model="loginForm.account"
            prefix-icon="el-icon-user"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
            type="password"
            placeholder="请输入密码"
            @keyup.enter.native="handleLogin"
          ></el-input>
        </el-form-item>
        <el-button type="primary" class="login-button" @click="handleLogin">登录</el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import qs from "qs";
export default {
  data() {
    return {
      loginForm: {
        account: "",
        password: "",
      },
      rules: {
        account: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    handleLogin() {
      let loginForm = qs.parse(qs.stringify(this.loginForm));
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$axios
            .post("/user/go", loginForm)
            .then((res) => {
              console.log(res);
              if (res.data.code == 0) {
                let userInfo = res.data.data;
                this.$message.success(`${userInfo.account}登录成功！`);
                this.$store.commit("login", userInfo);
                this.$router.push({ path: "/UserManager" });
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(() => {
              this.$message.error("请求失败");
            });
        }
      });
    },
  },
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-box {
  width: 400px;
  padding: 40px;
  border-radius: 15px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-logo {
  width: 80px;
  height: 80px;
  margin-bottom: 15px;
}

.login-title {
  margin: 0;
  color: #303133;
  font-size: 28px;
  font-weight: 600;
}

.login-button {
  width: 100%;
  margin-top: 30px;
  padding: 12px 0;
  font-size: 16px;
  border-radius: 8px;
  background: linear-gradient(to right, #667eea, #764ba2);
  border: none;
  transition: transform 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
}

:deep(.el-input__inner) {
  padding-left: 40px;
  height: 45px;
  line-height: 45px;
  border-radius: 8px;
  border: 1px solid #dcdfe6;
  transition: all 0.3s ease;
}

:deep(.el-input__inner:focus) {
  border-color: #764ba2;
  box-shadow: 0 0 0 2px rgba(118, 75, 162, 0.2);
}

:deep(.el-form-item) {
  margin-bottom: 25px;
}

:deep(.el-input__prefix) {
  left: 10px;
}
</style>